//
//  XS_QRCodeVC.swift
//  XueDao
//
//  Created by 韩云智 on 2021/1/13.
//

import UIKit
import RxCocoa
import RxSwift

class XS_QRCodeVC: XS_BaseVC {
    deinit {
        session?.stop()
    }
    var session: AVCaptureSessionManager?
    override func upConfig() {
        modalTransitionStyle = .crossDissolve
        modalPresentationStyle = .overFullScreen
        isStatusBarBlack = false
    }
    override func upView() {
        session = AVCaptureSessionManager(captureType: .AVCaptureTypeQRCode, scanRect: UIScreen.main.bounds) { [weak self] text in
            guard let `self` = self else { return }
            guard let text = text, text.hasPrefix("http") else {
                self.view.xsShowMsg("无法识别！")
                Driver.just(()).delay(.seconds(2))
                    .drive(onNext: { [weak self] in
                        self?.session?.start()
                    }).disposed(by: self.disposeBag)
                return
            }
            let vc = XS_WebPageVC(text)
            XS_NVC.shared.pushViewController(vc, animated: false)
            print(text)
            self.dismiss(animated: false)
        }
        session?.showPreViewLayerIn(view: view)
        loadCustomBar()
    }
}
